package com.neusoft.work.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.work.dao.CareLevelDao;
import com.neusoft.work.po.CareLevel;
import com.neusoft.work.po.PageResponseBean;
import com.neusoft.work.po.ResponseBean;
import com.neusoft.work.service.CareLevelService;

@Service
public class CareLevelServiceImpl implements CareLevelService {

    @Autowired
    private CareLevelDao careLevelMapper;

    @Override
    public PageResponseBean<List<CareLevel>> queryByStatus(Integer status, Long cur, Long number) {
        Page<CareLevel> page = new Page<>(cur, number);
        QueryWrapper<CareLevel> qw = new QueryWrapper<>();
        if (status != null) {
            qw.eq("status", status);
        }
        // 默认查询启用状态时，不传status也查询status=1
        if (status == null) {
            qw.eq("status", 1);
        }
        Page<CareLevel> result = careLevelMapper.selectPage(page, qw);
        PageResponseBean<List<CareLevel>> resp = new PageResponseBean<>();
        resp.setData(result.getRecords());
        resp.setTotal(result.getTotal());
        return resp;
    }

    @Override
    public ResponseBean<Integer> addCareLevel(CareLevel careLevel) {
        // 默认新建为启用
        if (careLevel.getStatus() == null) {
            careLevel.setStatus(1);
        }
        int rows = careLevelMapper.insert(careLevel);
        ResponseBean<Integer> resp = new ResponseBean<>();
        resp.setData(careLevel.getId());
        resp.setStatus(rows > 0 ? 200 : 500);
        resp.setMsg(rows > 0 ? "新增成功" : "新增失败");
        return resp;
    }

    @Override
    public ResponseBean<Integer> updateCareLevel(CareLevel careLevel) {
        // 仅允许修改状态
        CareLevel update = new CareLevel();
        update.setId(careLevel.getId());
        update.setStatus(careLevel.getStatus());
        int rows = careLevelMapper.updateById(update);
        ResponseBean<Integer> resp = new ResponseBean<>();
        resp.setData(careLevel.getId());
        resp.setStatus(rows > 0 ? 200 : 500);
        resp.setMsg(rows > 0 ? "更新成功" : "更新失败");
        return resp;
    }
}